﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Taobao.Tools2.Domain;
using WeifenLuo.WinFormsUI.Docking;

namespace Taobao.Tools2
{
    public partial class ShopConfigureForm : DockContent //Form
    {        
        #region Ctor
        public ShopConfigureForm()
        {
            InitializeComponent();
            DbContext = new Domain.EntityFramework.EfDbContext();
            ShopRepository = new Taobao.Tools2.Domain.EntityFramework.EntityRepository<Shop>(DbContext);
        }
        #endregion

        #region Properties
        protected Taobao.Tools2.Domain.EntityFramework.EfDbContext DbContext { get; set; }
        protected IRepository<Shop> ShopRepository { get; set; }
        #endregion

        #region Load
        private void ShopConfigureForm_Load(object sender, EventArgs e)
        {
            BindMainDataGrid();
        }
        #endregion

        #region MainDataGridView Events
        private void MainDataGridView_SelectionChanged(object sender, EventArgs e)
        {
            BindPanel();
        }
        #endregion

        #region 
        private void BindPanel()
        {
            if (MainDataGridView.CurrentRow != null)
            {
                var selectedRow = MainDataGridView.CurrentRow;
                var shop = selectedRow.DataBoundItem as Shop;
                if (shop != null)
                {
                    BindShop(shop);
                }
            }
        }
        private void BindShop(Shop shop)
        {
            IsUpdateDetailScheduledCheckBox.Checked = shop.IsUpdateDetailScheduled != null && shop.IsUpdateDetailScheduled.Value;
            ScheduledUpdateTimeDateTimePicker.Value = shop.ScheduledUpdateTime != null ? shop.ScheduledUpdateTime.Value
                                                                                       : DateTime.Now;
            ScheduledUpdateCountEachDayTextBox.Text = shop.ScheduledUpdateCountEachDay != null ? shop.ScheduledUpdateCountEachDay.Value.ToString() 
                                                                                               : "";
            BeforeMinutesOfUpdateByIncreamentTextBox.Text = shop.BeforeMinutesOfUpdateByIncreament != null ? shop.BeforeMinutesOfUpdateByIncreament.Value.ToString()
                                                                                                           : "";
        }
        #endregion

        #region Bind Grid
        private void BindMainDataGrid(int? index=null)
        {
            var allShops = ShopRepository.FindAll().ToList();
            MainDataGridView.AutoGenerateColumns = false;
            MainDataGridView.DataSource = allShops;
            if (index != null && index.Value>=0)
            {
                MainDataGridView.CurrentCell = MainDataGridView.Rows[index.Value].Cells[0];
                BindPanel();
            }
        }
        #endregion

        #region
        private void SaveButton_Click(object sender, EventArgs e)
        {
            if (MainDataGridView.CurrentRow != null)
            {
                try
                {
                    var selectedRow = MainDataGridView.CurrentRow;
                    var shop = selectedRow.DataBoundItem as Shop;
                    if (shop != null)
                    {
                        var dbShop = ShopRepository.FindAll().AsQueryable().Where(s => s.ID == shop.ID).SingleOrDefault();
                        if (dbShop != null)
                        {
                            dbShop.IsUpdateDetailScheduled = IsUpdateDetailScheduledCheckBox.Checked;
                            var scheduledUpdateCountEachDay = 1;
                            if (int.TryParse(ScheduledUpdateCountEachDayTextBox.Text, out scheduledUpdateCountEachDay) && scheduledUpdateCountEachDay > 0)
                            {
                                dbShop.ScheduledUpdateCountEachDay = scheduledUpdateCountEachDay;
                            }
                            var beforeMinutesOfUpdateByIncreament = 30;
                            if (int.TryParse(BeforeMinutesOfUpdateByIncreamentTextBox.Text, out beforeMinutesOfUpdateByIncreament) && beforeMinutesOfUpdateByIncreament > 0)
                            {
                                dbShop.BeforeMinutesOfUpdateByIncreament = beforeMinutesOfUpdateByIncreament;
                            }
                            dbShop.ScheduledUpdateTime = ScheduledUpdateTimeDateTimePicker.Value;
                            ShopRepository.Update(dbShop);
                            BindMainDataGrid(MainDataGridView.CurrentRow.Index);
                            MessageBox.Show(string.Format("配置店铺：{0}成功！", dbShop.Name));
                        }
                    }
                }
                catch
                {
                    MessageBox.Show(string.Format("配置店铺失败！"));
                }
            }
        }
        #endregion
    }
}
